﻿#region
/**
 * $Date: 0001/1/1
*/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using BuXiaChu.OrderDishes.DAL.DataAccess;
using BuXiaChu.OrderDishes.Model;
#endregion

namespace BuXiaChu.OrderDishes.DAL
{
    public class ShareCodeDAL
    {
        #region private const
        private const string PARAM_CODE_I = "@CodeID";
        private const string PARAM_CLASS_I = "@ClassID";
        private const string PARAM_NAME = "@Name";
        private const string PARAM_NAME_EN = "@NameEn";
        private const string PARAM_PARENT_ID = "@ParentId";
        private const string PARAM_CREATE_DATE = "@CreateDate";
        private const string PARAM_LAST_UPDATE_DATE = "@LastUpdateDate";
        private const string PARAM_START = "@Start";
        private const string PARAM_COUNT = "@Count";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        private static void FillModelFrom(SqlDataReader reader, ShareCodeModel obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.CodeID = reader.IsDBNull(reader.GetOrdinal("CodeID")) ? 0 : reader.GetInt64(reader.GetOrdinal("CodeID"));
                obj.ClassID = reader.IsDBNull(reader.GetOrdinal("ClassID")) ? 0 : reader.GetInt64(reader.GetOrdinal("ClassID"));
                obj.Name = reader.IsDBNull(reader.GetOrdinal("Name")) ? String.Empty : reader.GetString(reader.GetOrdinal("Name"));
                obj.NameEn = reader.IsDBNull(reader.GetOrdinal("NameEn")) ? String.Empty : reader.GetString(reader.GetOrdinal("NameEn"));
                obj.ParentId = reader.IsDBNull(reader.GetOrdinal("ParentId")) ? 0 : reader.GetInt64(reader.GetOrdinal("ParentId"));
                obj.CreateDate = reader.IsDBNull(reader.GetOrdinal("CreateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("CreateDate"));
                obj.LastUpdateDate = reader.IsDBNull(reader.GetOrdinal("LastUpdateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("LastUpdateDate"));
            }
            #endregion
        }

        private static SqlParameter[] GetShareCodeParams(ShareCodeModel obj)
        {
            #region
            SqlParameter[] dbParams ={					
					 SQLHelper.MakeParam(PARAM_CODE_I, SqlDbType.BigInt,0,obj.CodeID),
					 SQLHelper.MakeParam(PARAM_CLASS_I, SqlDbType.BigInt,0,obj.ClassID),
					 SQLHelper.MakeParam(PARAM_NAME, SqlDbType.NVarChar, 100,obj.Name),					
					 SQLHelper.MakeParam(PARAM_NAME_EN, SqlDbType.NVarChar, 100,obj.NameEn),					
					 SQLHelper.MakeParam(PARAM_PARENT_ID, SqlDbType.BigInt,0,obj.ParentId)
			};

            return dbParams;
            #endregion
        }

        public static bool Save(ShareCodeModel obj)
        {
            #region
            try
            {
                SqlParameter[] dbParams = GetShareCodeParams(obj);

                if (obj.CodeID == 0)
                {
                    obj.CodeID = Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "ShareCode_InsertUpdate", dbParams));
                    return obj.CodeID > 0 ? true : false;
                }
                else
                {
                    var affectNum = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "ShareCode_InsertUpdate", dbParams);
                    return affectNum > 0 ? true : false;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->InsertOrUpdate-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }

            return false;
            #endregion
        }

        public static ShareCodeModel GetByShareCodeId(long codeID)
        {
            #region
            SqlDataReader reader = null;
            ShareCodeModel obj = new ShareCodeModel();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CODE_I, SqlDbType.Int, 0, codeID)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ShareCode_GetCodeID", dbParams);

                if (reader.Read())
                {
                    FillModelFrom(reader, obj);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->GetSingleObject-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return obj;
            #endregion
        }

        public static  bool DeleteShareCode(long codeID)
        {
            #region
            //usp_ShareCodeClass_delete
            try
            {
                SqlParameter[] dbParams =
                {
						 SQLHelper.MakeParam(PARAM_CODE_I, SqlDbType.BigInt,0,codeID)
				};
                var AffectRows = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "ShareCode_Delete", dbParams);

                if (AffectRows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCodeClass-->DeleteShareCode-->" + e.Message);
                ExceptionMessageDAL.Record(ex);
            }
            return false;
            #endregion
        }

        public static List<ShareCodeModel> GetShareCodeListByClassID(long classID)
        {
            #region
            //usp_ShareCodeClass_delete
            var result = new List<ShareCodeModel>();
            try
            {
                SqlParameter[] dbParams = 
                {
                    SQLHelper.MakeParam(PARAM_CLASS_I,SqlDbType.BigInt,0,classID)
                };
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ShareCode_GetByClassID", dbParams))
                {
                    while (reader.Read())
                    {
                        var obj = new ShareCodeModel();
                        FillModelFrom(reader, obj);
                        result.Add(obj);
                    }
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->GetShareCodeListByClassID-->" + e.Message);
                ExceptionMessageDAL.Record(ex);
            }

            return result;

            #endregion
        }

        public static List<ShareCodeModel> GetShareCodeList()
        {
            #region
            var result = new List<ShareCodeModel>();
            try
            {
                var sqlSelect = "select * from ShareCode";
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.Text, sqlSelect, null))
                {
                    while (reader.Read())
                    {
                        var obj = new ShareCodeModel();
                        FillModelFrom(reader, obj);
                        result.Add(obj);
                    }
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->GetShareCodeList-->" + e.Message);
                ExceptionMessageDAL.Record(ex);
            }

            return result;

            #endregion
        }

        public static List<ShareCodeModel> GetShareCodeListByParentID(long parentID)
        {
            #region
            var result = new List<ShareCodeModel>();
            try
            {
                var sqlSelect = "select * from ShareCode where parentid=" + parentID;
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.Text, sqlSelect, null))
                {
                    while (reader.Read())
                    {
                        var obj = new ShareCodeModel();
                        FillModelFrom(reader, obj);
                        result.Add(obj);
                    }
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->GetShareCodeList-->" + e.ToString());
                ExceptionMessageDAL.Record(ex);
            }

            return result;
            #endregion
        }

        public static List<ShareCodeModel> GetListByParentIDandClassId(long parentID,long classId)
        {
            #region
            var result = new List<ShareCodeModel>();
            try
            {
                SqlParameter[] dbParams = new SqlParameter[] { 
                    SQLHelper.MakeParam(PARAM_CLASS_I,SqlDbType.BigInt,0,classId),
                    SQLHelper.MakeParam(PARAM_CODE_I,SqlDbType.BigInt,0,parentID)
                };
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ShareCode_GetByClassIDParentId", dbParams))
                {
                    while (reader.Read())
                    {
                        var obj = new ShareCodeModel();
                        FillModelFrom(reader, obj);
                        result.Add(obj);
                    }
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->GetListByParentIDandClassId-->" + e.ToString());
                ExceptionMessageDAL.Record(ex);
            }

            return result;
            #endregion
        }

    }
}
